package com.zl.learning.io.nio;

import org.junit.Test;

import java.io.FileInputStream;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.nio.CharBuffer;
import java.nio.MappedByteBuffer;
import java.nio.channels.FileChannel;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.nio.file.Paths;

import static java.nio.file.StandardOpenOption.READ;
import static java.nio.file.StandardOpenOption.WRITE;

public class TestBuffer {
    @Test
    public void test1() {
        ByteBuffer buffer = ByteBuffer.allocate(100).order(ByteOrder.BIG_ENDIAN);
        CharBuffer charBuffer = buffer.asCharBuffer();
        buffer.put((byte) 0).put((byte) 'H').put((byte) 0).put((byte) 'e').put((byte) 0).put((byte) 'l').put((byte) 0).put((byte) 'l').put((byte) 0).put((byte) 'o');
        System.out.println(charBuffer.toString());
        System.out.println(buffer.order(ByteOrder.BIG_ENDIAN).getInt(1));
    }

    @Test
    public void test2() throws IOException {
        FileChannel fileChannel = FileChannel.open(Paths.get("data", "hello.txt"), READ, WRITE);
        ByteBuffer byteBuffer = ByteBuffer.allocateDirect(1024);
        fileChannel.read(byteBuffer);
        System.out.println(byteBuffer.toString());
        byteBuffer.flip();
        System.out.println(byteBuffer.toString());
        byteBuffer.flip();
        System.out.println(byteBuffer.toString());
        byteBuffer.clear();
        System.out.println(byteBuffer.toString());
//        byteBuffer.put("编程中国".getBytes("UTF-8"));
//        while (byteBuffer.hasRemaining()) {
//            System.out.println(byteBuffer.get());
//        }
//        System.out.println(byteBuffer.toString());
        byteBuffer.clear();
        byteBuffer.limit(6);
        String a = Charset.forName("UTF-8").decode(byteBuffer).toString();
        System.out.println(a);
//        ByteBuffer b2 = ByteBuffer.wrap("Hello EveryBody!\n".getBytes("UTF-8"));
//        fileChannel.write(b2);
        System.out.println(byteBuffer.hasArray());
    }

    @Test
    public void test3() throws UnsupportedEncodingException {
        byte[] bytes = "我是中国人".getBytes(StandardCharsets.UTF_16BE);
        ByteBuffer byteBuffer = ByteBuffer.wrap(bytes);
        System.out.println(byteBuffer.toString());
        CharBuffer charBuffer = byteBuffer.asCharBuffer();
        System.out.println(charBuffer.toString());
        while (charBuffer.hasRemaining()) {
            System.out.println(charBuffer.get());
        }
    }

    @Test
    public void test4() throws IOException {
        FileChannel fileChannel = FileChannel.open(Paths.get("data", "hello.txt"), READ);
        ByteBuffer byteBuffer = ByteBuffer.allocateDirect(3);
//        int len = -1;
//        while ((len = fileChannel.read(byteBuffer)) > 0) {
//            byteBuffer.flip();
//            System.out.println(Charset.forName("UTF-8").decode(byteBuffer));
//            System.out.println(byteBuffer);
//            byteBuffer.flip();
//            System.out.println(len);
//        }
        System.out.println(fileChannel.size());
        for (int len = fileChannel.read(byteBuffer); len > 0; len = fileChannel.read(byteBuffer)) {
            System.out.println("file position - " + fileChannel.position());
            byteBuffer.flip();
            System.out.println(Charset.forName("UTF-8").decode(byteBuffer));
            System.out.println(byteBuffer);
            byteBuffer.flip();
            System.out.println(len);
        }
    }

    @Test
    public void test5() throws IOException {
        FileChannel fileChannel = FileChannel.open(Paths.get("C:\\Users\\zhaolei\\Pictures", "重生地球仙尊.txt"), READ);
        System.out.println(fileChannel.size());
        int pos = 0;
//        for (MappedByteBuffer byteBuffer = fileChannel.map(FileChannel.MapMode.READ_ONLY, pos, 1024); pos < fileChannel.size(); pos += 1024) {
//            System.out.println(Charset.forName("GBK").decode(byteBuffer));
//            byteBuffer = fileChannel.map(FileChannel.MapMode.READ_ONLY, pos, 1024);
//        }
        MappedByteBuffer byteBuffer = fileChannel.map(FileChannel.MapMode.READ_ONLY, 0, 1024);
        FileChannel target = FileChannel.open(Paths.get("data", "hello.txt"), WRITE);
        target.write(byteBuffer);
        target.close();
        fileChannel.close();
    }

    @Test
    public void test6() throws IOException {
        ByteBuffer byteBuffer = ByteBuffer.allocate(1024);
        byteBuffer.put("我是中国人\n".getBytes());
        System.out.println(byteBuffer.toString());
        byteBuffer.put("你从哪儿来？".getBytes());
        System.out.println(byteBuffer.toString());
        byteBuffer.flip();
        System.out.println(byteBuffer.toString());
        byteBuffer.rewind();
        System.out.println(byteBuffer.toString());

//        while (byteBuffer.hasRemaining()){
//            System.out.println(byteBuffer.get());
//        }
//        FileChannel channel = new FileOutputStream("1.txt").getChannel();
//        channel.write(byteBuffer);
//        channel.close();
    }

    @Test
    public void test7() throws IOException {
        FileChannel channel = new FileInputStream("1.txt").getChannel();
        ByteBuffer byteBuffer = ByteBuffer.allocate(1024);
        byte[] bytes = new byte[1024];
        int a;
        int i = 0;
        while ((a = channel.read(byteBuffer)) > 0) {
            byteBuffer.flip();
            while (byteBuffer.hasRemaining()) {
//                bytes[i] = byteBuffer.get();
//                i++;
                System.out.println(new String(byteBuffer.array()));
            }
            byteBuffer.clear();
        }
        System.out.println(a);
//        System.out.println(new String(bytes, 0, i));
        channel.close();
    }

    @Test
    public void test8() {
        ByteBuffer byteBuffer = ByteBuffer.wrap("HelloWorld".getBytes());
        System.out.println(byteBuffer);
    }
}
